<template>
  <div class="wrap">
    <div class="searchBox">
      <el-input
        prefix-icon="el-icon-search"
        v-model="searchTableInfo"
        placeholder="请输入搜索内容"
        style="width:30%"
        clearable
      ></el-input>
    </div>
    <!-- 列表 -->
    <el-table
      ref="table"
      :data="tableData.slice((currpage - 1) * pagesize, currpage * pagesize)"
      border
      style="width: 100%"
      stripe
      @selection-change="handleSelectionChange"
      @sort-change="changeSort"
      :row-key="getRowKeys"
    >
      <el-table-column
        type="selection"
        width="40"
        align="center"
        :reserve-selection="true"
      ></el-table-column>
      <el-table-column label="协议" prop="ptl" min-width="80" align="center">
        <template slot-scope="scope">{{ scope.row.ptl }}</template>
      </el-table-column>
      <el-table-column
        label="通道号"
        prop="channel"
        min-width="80"
        align="center"
      >
        <template slot-scope="scope">{{
          scope.row.channel
        }}</template>
      </el-table-column>
      <el-table-column label="码流" prop="sub" width="60" align="center">
        <template slot-scope="scope">{{ scope.row.sub }}</template>
      </el-table-column>
      <el-table-column label="URL" prop="url" width="680" align="center">
        <template slot-scope="scope">{{ scope.row.url }}</template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        prop="created_time"
        width="180"
        align="center"
      >
        <template slot-scope="scope">{{
          scope.row.created_time
        }}</template>
      </el-table-column>
      <!-- <el-table-column label="最近修改" prop="limited_type" width="160" align="center">
        <template slot-scope="scope">{{scope.row.sid}}</template>
      </el-table-column>-->
      <el-table-column
        label="流量限制类型"
        prop="limited_type"
        width="160"
        align="center"
        >使用限制</el-table-column
      >
      <el-table-column
        class="play-box"
        label="操作"
        min-width="160"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <!-- <el-button
            type="primary"
            size="small"
            icon="el-icon-video-play"
            @click="play(scope.$index, scope.row)"
          >播放</el-button> -->
          <el-button
            size="small"
            type="success"
            @click="modify(scope.$index, scope.row)"
            >修改</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="delRow(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      right
      background
      layout="total,prev, pager, next, sizes,jumper"
      :page-sizes="[5, 10, 15, 25]"
      :page-size="pagesize"
      :total="tableData.length"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>

    <!-- 点击授权，弹出框 -->
    <ApplyBillsModitify
      :dialogVisible="dialogVisible"
      @check="fnCheck"
      @all="fnAll"
      @other="fnOther"
      @success="fnSuccess"
      v-if="ticket !== ''"
      :ticket="ticket"
      :url="url"
    />
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";
import ApplyBillsModitify from "./ApplyBillsModitify";
export default {
  name: "",
  components: { ApplyBillsModitify },
  props: {},
  data() {
    return {
      currpage: 1, //分页
      pagesize: 5, //分页
      searchTableInfo: "", //搜索条件
      getSearchInfo: [], //搜索后，过滤后的tableData数据
      tableChecked: [], // 多选
      batchDelectId: [], //批量删除id
      serialNum: "",
      checkAll: false, // 是否全选状态
      delBtnShow: false,
      //授权，弹出框
      dialogVisible: {
        show: false,
      },
      ticket: "", //票据id
      url: "", //票据url
    };
  },
  computed: {
    // 根据计算属性模糊搜索
    tableData() {
      const searchTableInfo = this.searchTableInfo;
      //console.log(searchTableInfo);
      if (searchTableInfo) {
        return this.getSearchInfo.filter((item) => {
          return Object.keys(item).some((key) => {
            return (
              String(item[key])
                .toLowerCase()
                .indexOf(searchTableInfo) > -1
            );
          });
        });
      }
      return this.getSearchInfo;
    },
    //点击添加组件组件时候，刷新table
    submitData() {
      return this.$store.state.submitData;
    },
  },
  created() {},
  watch: {
    submitData(newVal, oldVal) {
      if (newVal != oldVal) {
        this.getList();
        this.currpage = 1;
      }
    },
  },
  mounted() {
    this.sn = this.$route.query.sn;
    console.log(this.sn);

    this.getList();
  },
  methods: {
    //获取初始列表数据
    getList() {
      postData("/adminweb/device/stream/live/ticket/list", {
        sn: this.sn,
      })
        .then((res) => {
          console.log(res);
          if (res.code == 0) {
            let tickets = res.datas.tickets;
            this.getSearchInfo = tickets;
          } else if (res.code == -56055) {
            return;
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          this.$message.error(res.code + "，" + res.msg);
        });
    },
    // 获取row的key值
    getRowKeys(row) {
      return row.serialNum;
    },
    //点击 第几页
    handleCurrentChange(cpage) {
      this.currpage = cpage;
    },
    //每页有几条
    handleSizeChange(psize) {
      this.pagesize = psize;
    },
    //操作多选
    handleSelectionChange(val) {
      var that = this;
      this.batchDelectId = [];
      that.tableChecked = val;
      console.log("全选");
      console.log(that.tableChecked);
      if (val.length > 0) {
        for (let i = 0; i < val.length; i++) {
          this.batchDelectId.push(val[i]["role_id"]);
        }
      }
    },
    //排序
    changeSort(val) {
      console.log(val);
    },
    //删除单行
    delRow(index, rows) {
      console.log(rows);
      var subArr = [];
      subArr.push(rows.ticket);
      var that = this;
      that
        .$confirm("此操作将删除:" + rows.url + ", 是否继续?", "删除", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          postData("/adminweb/device/stream/live/ticket/delete", {
            tickets: subArr,
          })
            .then((res) => {
              that.logining = false;
              console.log(res);
              if (res.code == "0") {
                 that.$message.success("删除成功")
                that.getList();
              } else {
                that.$message.error(res.code + ":" + res.msg);
              }
            })
            .catch((res) => {
              that.$message.error(res);
            });
        })
        .catch((res) => {
          that.$message.success("已取消");
        });
    },

    //点击授权弹窗
    modify(index, row) {
      console.log(row);
      this.dialogVisible = { show: true };
      this.ticket = row.ticket;
      this.url = row.url;
    },
    //修改弹窗，选单个
    fnCheck(val) {
      //console.log(val);
    },
    //修改弹窗，全选
    fnAll(val) {
      //console.log(val);
    },
    //修改弹窗，反选
    fnOther(val) {
      //console.log(val);
    },
    // 授权成功
    fnSuccess() {
      this.dialogVisible = {
        show: false,
      };
    },
  },
};
</script>
<style scoped>
/* 更改table,表头背景 */
.wrap >>> .el-table--border th,
.el-table__fixed-right-patch {
  /* background: #c0dee2; */
  background: #f0f3f4;
}
.wrap {
  margin-top: 20px;
  overflow: hidden;
  padding: 15px;
}
.searchBox {
  margin-bottom: 20px;
}
.wrap >>> .el-pagination {
  float: right;
  margin-top: 30px;
}

.batch-btn-con {
  /* background: #fff; */
  margin-top: 70px;
}
.wrap >>> .tips {
  margin-top: 10px;
  display: block;
  font-size: 15px;
}
</style>
